<script setup lang="ts">
import { computed, reactive, watch } from 'vue';
import { $t } from '@/locales';
import { enableStatusRecord } from '@/constants/business';

defineOptions({
  name: 'ShowDetailModal'
});

interface Props {
  /** row data */
  rowData?: Api.SystemManage.Post | null;
}

const props = defineProps<Props>();

const visible = defineModel<boolean>('visible', {
  default: false
});

const title = computed(() => $t('page.manage.position.postDetail'));

function closeModal() {
  visible.value = false;
}

// 创建响应式数据模型
type DetailModel = Pick<Api.SystemManage.Post, 'postId' | 'postName' | 'postCode' | 'deptId' | 'deptName' | 'status'>;

const detailModel: DetailModel = reactive({
  postId: '',
  postName: '',
  postCode: '',
  deptId: null,
  deptName: '',
  status: 1
});

function handleInitModel() {
  if (props.rowData) {
    // 确保所有字段都有正确的默认值处理
    Object.assign(detailModel, {
      postId: props.rowData.postId || '',
      postName: props.rowData.postName || '',
      postCode: props.rowData.postCode || '',
      deptId: props.rowData.deptId || null,
      deptName: props.rowData.deptName || '',
      status: props.rowData.status ?? 1
    });
  }
}

watch(visible, () => {
  if (visible.value) {
    handleInitModel();
  }
});
</script>

<template>
  <NModal v-model:show="visible" preset="dialog" :title="title" style="width: 800px; max-width: 90vw">
    <div class="space-y-16px">
      <NForm ref="formRef" :model="detailModel" label-placement="left" :label-width="100" disabled>
        <NGrid responsive="screen" item-responsive>
          <NFormItemGi span="24 m:12" :label="$t('page.manage.position.postId')" path="postId">
            <NInput v-model:value="detailModel.postId" />
          </NFormItemGi>

          <NFormItemGi span="24 m:12" :label="$t('page.manage.position.postName')" path="postName">
            <NInput v-model:value="detailModel.postName" />
          </NFormItemGi>

          <NFormItemGi span="24 m:12" :label="$t('page.manage.position.postCode')" path="postCode">
            <NInput v-model:value="detailModel.postCode" />
          </NFormItemGi>

          <NFormItemGi span="24 m:12" :label="$t('page.manage.position.belongDept')" path="deptName">
            <NInput v-model:value="detailModel.deptName" />
          </NFormItemGi>

          <NFormItemGi span="24 m:12" :label="$t('page.manage.position.status')" path="status">
            <NTag :type="detailModel.status === 1 ? 'success' : 'warning'">
              {{ detailModel.status ? $t(enableStatusRecord[detailModel.status]) : '' }}
            </NTag>
          </NFormItemGi>
        </NGrid>
      </NForm>
    </div>

    <template #action>
      <NSpace justify="end">
        <NButton @click="closeModal">{{ $t('common.close') }}</NButton>
      </NSpace>
    </template>
  </NModal>
</template>

<style scoped></style>
